package com.leyou.common.bean;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author zqq
 * @ClassName Result
 * @Description: 统一返回结果对象
 * @date 2020/3/5-10:13
 */
@Data
@ApiModel(value = "Result", description = "统一返回结果对象")
public class Result<T> implements Serializable {

    /** 状态码 */
    @ApiModelProperty(value = "状态码", name = "code")
    private int code;

    /** 返回结果 */
    @ApiModelProperty(value = "返回结果", name = "data")
    private T data;

    /** 返回描述 */
    @ApiModelProperty(value = "返回描述", name = "message")
    private String message;

    /**
     * @Title: newSuccess
     * @Description: 接口调用成功，不需要返回对象
     * @author: zqq
     * @param
     * @return: com.leyou.common.bean.Result<T>
     * @createTime: 2020-03-05 10:37
     * @version: v.1.0
     */
    public static <T> Result<T> newSuccess() {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMsg());
        return result;
    }

    /**
     * @Title: newSuccess
     * @Description: 接口调用成功，有返回对象
     * @author: zqq
     * @param object
     * @return: com.leyou.common.bean.Result<T>
     * @createTime: 2020-03-05 10:39
     * @version: v.1.0
     */
    public static <T> Result<T> newSuccess(T object) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMsg());
        result.setData(object);
        return result;
    }

    /**
     * @Title: newFailure
     * @Description: 接口调用失败，有错误码和描述，没有返回对象
     * @author: zqq
     * @param code
     * @return: com.leyou.common.bean.Result<T>
     * @createTime: 2020-03-05 10:40
     * @version: v.1.0
     */
    public static <T> Result<T> newFailure(ResultCode code) {
        Result<T> result = new Result<>();
        result.setCode(code.getCode());
        result.setMessage(code.getMsg());
        return result;
    }

    /**
     * @Title: newFailure
     * @Description: 接口调用失败，有错误码和描述，没有返回对象
     * @author: zqq
     * @param code
     * @return: com.leyou.common.bean.Result<T>
     * @createTime: 2020-03-05 10:40
     * @version: v.1.0
     */
    public static <T> Result<T> newFailure(int code,String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * @Title: success
     * @Description: 判断是否返回成功
     * @author: zqq
     * @param
     * @return: boolean
     * @createTime: 2020-03-05 10:41
     * @version: v.1.0
     */
    public boolean success() {
        return code == 1;
    }

    /**
     * @Title: hasObject
     * @Description: 判断是否返回成功，并且结果是否为空
     * @author: zqq
     * @param
     * @return: boolean
     * @createTime: 2020-03-05 10:42
     * @version: v.1.0
     */
    public boolean hasObject() {
        return code == 1 && data != null;
    }

    private static final long serialVersionUID = 1L;
}
